package code.rocky.consumer;

public class ConsumerDemo06 {

    private static Object lock = new Object();


    public static void main(String[] args) {
        ConsumerDemo06 demo06 = new ConsumerDemo06();
        demo06.startThreadA();
    }

    public void startThreadA() {
        new Thread(() -> {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + ": get lock");
                startThreadB();
                System.out.println(Thread.currentThread().getName() + ": start wait");
                try {
                    lock.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ": get lock after wait");
                System.out.println(Thread.currentThread().getName() + ": release lock");

            }
        }, "thread-a").start();
    }

    private void startThreadB() {
        new Thread(() -> {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + ": get lock");
                startThreadC();
                try {
                    Thread.sleep(200);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ": start notify");
                lock.notify();
                System.out.println(Thread.currentThread().getName() + ": release lock");

            }
        }, "thread-b").start();
    }

    private void startThreadC() {
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + ":thread c start ");

            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + ": get lock");
                System.out.println(Thread.currentThread().getName() + ": release lock");
            }
        }, "thread-c").start();
    }

}
